package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;

import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Menu;

import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 菜单管理
 */
@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

//分页查询
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        //完成分页查询,基于mybatis框架提供的分页助手
        PageHelper.startPage(currentPage,pageSize);
        Page<Menu> page = menuDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Menu> rows = page.getResult();
        return new PageResult(total,rows);
    }

    //根据id删除菜单

    public void delete(Integer id) {
        long count = menuDao.findCountByMenuId(id);

        if (count > 0) {
            new RuntimeException();
        }
       menuDao.delete(id);

    }

//根据id查询菜单用于回显
    public Menu findById(Integer id) {
        Menu menu = menuDao.findById(id);
        return menu;
    }




    public List<Menu> findAll() {
        List<Menu> menu= menuDao.findAll();
        return menu;
    }

    /**
     * 新增菜单
     * @param menu
     */
    public void add(Menu menu) {
        menuDao.add(menu);
    }

    @Override
    public void update(Menu menu) {

        menuDao.update(menu);

    }
}
